package io.chenglicun.modules.clcLocationMark.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.chenglicun.common.service.impl.CrudServiceImpl;
import io.chenglicun.common.constant.Constant;
import io.chenglicun.common.utils.ConvertUtils;
import io.chenglicun.modules.clc.service.ClcMapNodeInfoService;
import io.chenglicun.modules.clcHousePersonInfo.dto.ClcHousePersonInfoDTO;
import io.chenglicun.modules.clcHousePersonInfo.entity.ClcHousePersonInfoEntity;
import io.chenglicun.modules.clcHousePersonInfo.service.ClcHousePersonInfoService;
import io.chenglicun.modules.clcLocationMark.dao.ClcLocationMarkDao;
import io.chenglicun.modules.clcLocationMark.dto.ClcLocationMarkDTO;
import io.chenglicun.modules.clcLocationMark.entity.ClcLocationMarkEntity;
import io.chenglicun.modules.clcLocationMark.service.ClcLocationMarkService;
import io.chenglicun.modules.security.user.SecurityUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 地图标注
 *
 * @author HB 137664951@qq.com
 * @since 1.0 2024-01-22
 */
@Service
public class ClcLocationMarkServiceImpl extends CrudServiceImpl<ClcLocationMarkDao, ClcLocationMarkEntity, ClcLocationMarkDTO> implements ClcLocationMarkService {

    @Resource
    private ClcHousePersonInfoService clcHousePersonInfoService;

    @Resource
    private ClcMapNodeInfoService clcMapNodeInfoService;
    @Override
    public QueryWrapper<ClcLocationMarkEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<ClcLocationMarkEntity> wrapper = new QueryWrapper<>();

        if(null != params.get("locationMarkId") && !"".equals(params.get("locationMarkId").toString())) {
            wrapper.lambda().eq(ClcLocationMarkEntity::getLocationMarkId, params.get("locationMarkId"));
        }

        if(null != params.get("girdId") && !"".equals(params.get("girdId").toString())) {
            wrapper.lambda().eq(ClcLocationMarkEntity::getGirdId, params.get("girdId"));
        }
        if(null != params.get("gridOfVillageId") && !"".equals(params.get("gridOfVillageId").toString())) {
            wrapper.lambda().eq(ClcLocationMarkEntity::getGridOfVillageId, params.get("gridOfVillageId"));
        }
        if(null != params.get("status") && !"".equals(params.get("status").toString())) {
            wrapper.lambda().eq(ClcLocationMarkEntity::getStatus, params.get("status"));
        }

        if(null != params.get("locationType") && !"".equals(params.get("locationType").toString())) {
            wrapper.lambda().eq(ClcLocationMarkEntity::getLocationType, params.get("locationType"));
        }
        return wrapper;
    }


    @Override
    public ClcLocationMarkDTO getByMarkId(String markId) {
        Map<String, Object> params = new HashMap<>();
        params.put("location_mark_id", markId);
        List<ClcLocationMarkEntity> clcLocationMarkList = baseDao.selectByMap(params);
        if(clcLocationMarkList == null || clcLocationMarkList.isEmpty()) {
            return null;
        }
        //取第一个
        ClcLocationMarkDTO dto = ConvertUtils.sourceToTarget(clcLocationMarkList.get(0), ClcLocationMarkDTO.class);
        Map<String, Object> personInfoParams = new HashMap<>();
        params.put("houseId", dto.getId());
        List<ClcHousePersonInfoDTO> personInfoList = clcHousePersonInfoService.list(personInfoParams);
        dto.setPersonInfoList(personInfoList);
        return dto;
    }

    @Override
    public void delete(Long[] ids) {
        super.delete(ids);
        clcMapNodeInfoService.deleteByPointIds(ids);
    }
}